package io.github.loan.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import io.github.loan.common.util.DateFormatUtil;
import io.github.loan.common.util.EncryptUtil;
import io.github.loan.common.util.PageInfoUtil;
import io.github.loan.common.util.RegexUtil;
import io.github.loan.common.util.VerifyCodeUtils;


public class BaseController {
	
	@Resource 
	protected EncryptUtil encryptUtil;
	@Resource
	protected RegexUtil regexUtil;
	@Resource
	protected DateFormatUtil dateFormatUtil;
	protected String info;
	
	
	/**
	 * 生成验证码
	 */
	@RequestMapping("/code.mvc")
	public String code(HttpServletRequest request,HttpServletResponse response){
		HttpSession session= request.getSession();
		String code=this.regexUtil.randString(4);
		try{
			VerifyCodeUtils.outputImage(100, 50, response.getOutputStream(), code);
		} catch (IOException e) {
			e.printStackTrace();
		}
		/* 将图片中的验证码放到session中,用来验证 */
		session.setAttribute("code", code);
		return null;
	}
	
	/**
	 * 生成分页对象
	 * @param request
	 * @return
	 */
	protected PageInfoUtil proccedPageInfo(HttpServletRequest request) {
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		try {
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e) {
		}
		return pageInfoUtil;
	}

	/**
	 * 拼装JSON JUI框架需要的JSON
	 * 
	 * String res = "{\"statusCode\":\"200\", \"message\":\""+ info +"\",
	 * \"navTabId\":\"\", \"rel\":\"\", \"callbackType\":\"\",
	 * \"forwardUrl\":\"\", \"confirmMsg\":\"\"}" ;
	 * 
	 * @param request
	 * @param info
	 * @return
	 */
	protected JSONObject returnJuiJSON(HttpServletRequest request, String info) {
		/* JSON中的其它信息从request中获取,(JSP页面上) */
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String callbackType = request.getParameter("callbackType");
		String forwardUrl = request.getParameter("forwardUrl");
		String confirmMsg = request.getParameter("confirmMsg");

		if (navTabId == null) {
			navTabId = "";
		}
		if (rel == null) {
			rel = "";
		}
		if (callbackType == null) {
			callbackType = "";
		}
		if (forwardUrl == null) {
			forwardUrl = "";
		}
		if (confirmMsg == null) {
			confirmMsg = "";
		}
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", info);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON;
	}
}
